package three_shang.智能算法;

import java.util.Arrays;
import java.util.Random;

public class GeneticAlgorithmDemo {

    private static final int POPULATION_SIZE = 100;
    private static final int CHROMOSOME_LENGTH = 10;
    private static final int MAX_GENERATIONS = 1000;
    private static final double MUTATION_RATE = 0.01;

    public static void main(String[] args) {
        int[][] population = initializePopulation();
        Random random = new Random();

        for (int generation = 0; generation < MAX_GENERATIONS; generation++) {
            int[][] nextGeneration = new int[POPULATION_SIZE][CHROMOSOME_LENGTH];

            // 评估适应度并选择父代
            int[][] parents = selectParents(population, random);

            // 交叉生成下一代
            for (int i = 0; i < POPULATION_SIZE; i++) {
                int[] child = crossover(parents[random.nextInt(parents.length)], parents[random.nextInt(parents.length)], random);
                nextGeneration[i] = child;
            }

            // 变异
            for (int i = 0; i < POPULATION_SIZE; i++) {
                if (random.nextDouble() < MUTATION_RATE) {
                    mutate(nextGeneration[i], random);
                }
            }

            population = nextGeneration;
        }

        // 从最终种群中找到最大值
        int maxFitness = Integer.MIN_VALUE;
        int[] fittestIndividual = null;
        for (int[] individual : population) {
            int fitness = fitness(individual);
            if (fitness > maxFitness) {
                maxFitness = fitness;
                fittestIndividual = individual;
            }
        }

        System.out.println("最大值为: " + maxFitness);
        System.out.println("对应的染色体为: " + Arrays.toString(fittestIndividual));
    }

    private static int[][] initializePopulation() {
        int[][] population = new int[POPULATION_SIZE][CHROMOSOME_LENGTH];
        Random random = new Random();
        for (int i = 0; i < POPULATION_SIZE; i++) {
            for (int j = 0; j < CHROMOSOME_LENGTH; j++) {
                population[i][j] = random.nextInt(100); // 随机初始化染色体
            }
        }
        return population;
    }

    private static int[][] selectParents(int[][] population, Random random) {
        // 简单地随机选择父代
        int[][] parents = new int[2][];
        parents[0] = population[random.nextInt(POPULATION_SIZE)];
        parents[1] = population[random.nextInt(POPULATION_SIZE)];
        return parents;
    }

    private static int[] crossover(int[] parent1, int[] parent2, Random random) {
        // 交叉点为数组的中间位置
        int crossoverPoint = CHROMOSOME_LENGTH / 2;
        int[] child = new int[CHROMOSOME_LENGTH];
        for (int i = 0; i < CHROMOSOME_LENGTH; i++) {
            child[i] = i < crossoverPoint ? parent1[i] : parent2[i];
        }
        return child;
    }

    private static void mutate(int[] individual, Random random) {
        // 随机选择一个基因位置进行变异
        int mutationPoint = random.nextInt(CHROMOSOME_LENGTH);
        individual[mutationPoint] = random.nextInt(100);
    }

    private static int fitness(int[] individual) {
        // 适应度函数为数组中的最大值
        int max = Integer.MIN_VALUE;
        for (int gene : individual) {
            if (gene > max) {
                max = gene;
            }
        }
        return max;
    }
}

